let goods_msg={};
  setPage();
  $('.container').on('click','[name="cart"]',function(){
    const cookieObj=myGetCookie();
    if(cookieObj.login === undefined){
      if(window.confirm('你还没有登录，点击确定，跳转登录页面')){
      window.location.href=`./login.html?${window.location.href}`;
      }
    }else{
      let cart=window.localStorage.getItem('cart');
      if(cart === null){
        goods_msg.buy=true;
        goods_msg.num=$('[name="num"]').val()-0;
        console.log(goods_msg);

        window.localStorage.setItem('cart',JSON.stringify([goods_msg]));
      }else{
        cart=JSON.parse(cart);
        if(cart.length === 0){
        goods_msg.buy=true;
        goods_msg.num=$('[name="num"]').val()-0;
        console.log(goods_msg);

        cart.unshift(goods_msg);
        window.localStorage.setItem('cart',JSON.stringify(cart));
        }else{
          let bool=cart.some(function(val){
          return val.goods_id === goods_msg.goods_id;
          });
          console.log(bool);
          if(bool){
             for(let i=0;i<=cart.length-1;i++){
               if(cart[i].goods_id === goods_msg.goods_id){
                 cart[i].num=cart[i].num+($('[name="num"]').val()-0) >goods_msg.goods_number ? goods_msg.goods_number : cart[i].num +($('[name="num"]').val()-0);
                 break;
               }
             }
             window.localStorage.setItem('cart',JSON.stringify(cart));
          }else{
            goods_msg.buy=true;
            goods_msg.num=$('[name="num"]').val()-0;
            cart.unshift(goods_msg);
            window.localStorage.setItem('cart',JSON.stringify(cart));
          }
        }
      }
      window.location.href='./cart.html';
    }
  });

    $('.container').click(function(e){
    if(e.target.getAttribute('name') === '-'){
      $('[name="num"]').val($('[name="num"]').val()-0-1 < 1 ? 1 : $('[name="num"]').val()-0-1);
    }else if(e.target.getAttribute('name') === '+'){
      $('[name="num"]').val($('[name="num"]').val()-0+1 >goods_msg.goods_number-0 ? goods_msg.goods_number-0 :$('[name="num"]').val()-0+1);
    }
    });

    $('.container').on('blur','[name="num"]',function(){
    if($('[name="num"]').val()-0 <1){
    $('[name="num"]').val(1);
    }else if($('[name="num"]').val()-0 > goods_msg.goods_number-0){
    $('[name="num"]').val(goods_msg.goods_number-0);
    }
    });

    async function setPage(){
      const urlObj=myGetUrlVal();
      console.log(urlObj);

      const res=await myPromiseAjax('../server/goods_detail.php','post',{goods_id:urlObj.goods_id},'json');
      console.log(res);
      goods_msg=res;

      let str=
      `
            <div class="panel panel-default">
        <div class="panel-heading">
          <h3 class="panel-title">商品详细信息</h3>
        </div>
        <div class="panel-body">
          <div class="media">
            <div class="media-left">
              <a href="#">
                <img class="media-object" src="${res.goods_small_logo}" alt="...">
              </a>
            </div>
            <div class="media-body">
              <h4 class="media-heading">${res.goods_name}</h4>
              <p>
                <i class="glyphicon glyphicon-yen"></i>
                <span>${res.goods_price}</span>
              </p>
              <div class="btn-group" role="group" aria-label="...">
                <button type="button" class="btn btn-default">XL</button>
                <button type="button" class="btn btn-default">L</button>
                <button type="button" class="btn btn-default">M</button>
                <button type="button" class="btn btn-default">S</button>
                <button type="button" class="btn btn-default">XS</button>
              </div>
              <p style="position:relative">
                <input value="1" class="num" name="num">
                <button type="button" name="-" class="jian btn btn-default">-</button>
                <button type="button" name="+" class="jia btn btn-default">+</button>
              </p>
              <p>
                <a href="javascript:;" class="btn btn-warning btn-lg" role="button">立即购买</a>
                <a href="javascript:;" name="cart" class="btn btn-danger btn-lg" role="button">加入购物车</a>
              </p>
            </div>
          </div>
          <ul class="nav nav-tabs">
            <li role="presentation" class="active"><a href="#">商品详细信息</a></li>
            <li role="presentation"><a href="#">商品参数信息</a></li>
            <li role="presentation"><a href="#">相关商品</a></li>
          </ul>
          <div>
              ${res.goods_introduce}
          </div>
        </div>
      </div>
      `;
      $('.container').html(str);
    }